package com.dylan.历史.多线程.并发编程的艺术.锁;

import com.dylan.历史.util.LogUtil;
import com.dylan.历史.util.SleepUtils;

import java.util.LinkedList;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;

/**
 * Created by Dylan on 2018/9/19.
 */
public class MutexTest {

    public static void main(String[] args) {
        Producer producer = new Producer();
        Thread produceThread = new Thread(producer,"producer");
        produceThread.start();
        Consumer consumer = new Consumer();
        Thread consumeThread = new Thread(consumer, "consumer");
        consumeThread.start();

        SleepUtils.seconds(20);
        producer.cancel();
        produceThread.interrupt();
        consumer.cancel();
        consumeThread.interrupt();
    }

    private static LinkedList<String> goods = new LinkedList<>();

    private static Mutex lock = new Mutex();

    private static Condition consumerWaiter = lock.newCondition();
    private static Condition producerWaiter = lock.newCondition();

    private static AtomicInteger count = new AtomicInteger();

    private static class Producer implements Runnable {
        private volatile boolean running = true;

        @Override
        public void run() {
                lock.lock();
                try {
                    while (running) {
                        SleepUtils.seconds(1);
                        String g = "第" + count.incrementAndGet() + "商品 " + UUID.randomUUID();
                        LogUtil.info("生产商品：" + g);
                        goods.add(g);
                        try {
                            // 生产者提醒消费者消费
                            consumerWaiter.signalAll();
                            // 生产者生产之后进入等待状态
                            producerWaiter.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                } finally {
                    lock.unlock();
                }
                LogUtil.info("生产者结束");
        }

        public void cancel() {
            running = false;
        }
    }

    private static class Consumer implements Runnable {
        private volatile boolean running = true;

        @Override
        public void run() {
            lock.lock();
            try {
                while (running) {
                    String g =  goods.poll();
                    if (g == null) {
                        try {
                            // 消费者提醒生产者生产
                            producerWaiter.signalAll();
                            // 消费者消费完之后进入等待
                            consumerWaiter.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    } else {
                        SleepUtils.seconds(1);
                        LogUtil.info("消费商品：" + g);
                    }
                }
            } finally {
                lock.unlock();
            }
            LogUtil.info("消费者结束");
        }

        public void cancel() {
            running = false;
        }
    }
}
